package com.spsy.pay.sign;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.security.MessageDigest;
import java.util.Arrays;
import java.util.Map;
import java.util.Set;


/**
 * @author: liuzhiping
 * @date: 2024/12/20 16:04
 **/
public class SignUtil {


    public static String key = "3w24y82r4y23@";

    /****
     *
     * @param map<Key:value> = {"b":"1", "c":"2", "a":"3", "d":"4"}
     * @param key 秘钥
     * @param signType 签名方式 MD5 ,
     * @return
     */
    public static String genSign(final Map<String,String> map, String key, String signType) throws Exception{

        //获取key的集合  b,c,a,d
        Set<String> keySet = map.keySet();

        //new String[]{b,c,a,d} 新创建的字符串
        String[] arraysStr = keySet.toArray(new String[keySet.size()]);

        //new String[]{a,b,c,d}
        Arrays.sort(arraysStr);

        //
        StringBuffer buffer = new StringBuffer();
        for(String k : arraysStr){
            if (k.equals("sign")) {
                continue;
            }
            String value = map.get(k);//a
            System.out.println("value:"+value);//3,1,2,4

            buffer.append(k).append("=").append(value);
            buffer.append("@");
        }
        //String stringA = buffer.toString();
        //a=3&b=1&c=2&d=4& ===> buffer.toString()

        buffer.append("key").append("=").append(key).append("@bw=bw");

        //a=3&b=1&c=2&d=4&key=xxxx ====> buffer.toString()

        String sign = "";
        if(signType.equals("MD5")){
            sign = MD5(buffer.toString()).toUpperCase();
        }else if(signType.equals("HMACSHA256")){
            sign = HMACSHA256(buffer.toString(),key);
        }else{
            throw  new Exception("signType exception");
        }
        return sign;

    }





    /**
     * 生成 MD5
     *
     * @param data
     *            待处理数据
     * @return MD5结果
     */
    public static String MD5(String data) throws Exception {
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] array = md.digest(data.getBytes("UTF-8"));
        StringBuilder sb = new StringBuilder();
        for (byte item : array) {
            sb.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3));
        }
        return sb.toString().toUpperCase();
    }

    /**
     * 生成 HMACSHA256
     *
     * @param data
     *            待处理数据
     * @param key
     *            密钥
     * @return 加密结果
     * @throws Exception
     */
    public static String HMACSHA256(String data, String key) throws Exception {
        Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
        SecretKeySpec secret_key = new SecretKeySpec(key.getBytes("UTF-8"), "HmacSHA256");
        sha256_HMAC.init(secret_key);
        byte[] array = sha256_HMAC.doFinal(data.getBytes("UTF-8"));
        StringBuilder sb = new StringBuilder();
        for (byte item : array) {
            sb.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3));
        }
        return sb.toString().toUpperCase();
    }
}
